package cn.stylefeng.guns.modular.yingbao.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.yingbao.entity.Issue;
import cn.stylefeng.guns.modular.yingbao.mapper.IssueMapper;
import cn.stylefeng.guns.modular.yingbao.model.params.IssueParam;
import cn.stylefeng.guns.modular.yingbao.model.result.IssueResult;
import  cn.stylefeng.guns.modular.yingbao.service.IssueService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 常见问题 服务实现类
 * </p>
 *
 * @author zs
 * @since 2019-12-13
 */
@Service
public class IssueServiceImpl extends ServiceImpl<IssueMapper, Issue> implements IssueService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(IssueParam param){
        Issue entity = getEntity(param);
        this.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(IssueParam param){
        this.removeById(getKey(param));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(IssueParam param){
        Issue oldEntity = getOldEntity(param);
        Issue newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public IssueResult findBySpec(IssueParam param){
        return null;
    }

    @Override
    public List<IssueResult> findListBySpec(IssueParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(IssueParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public ResponseData queryAll() {
        List<Issue> issues = this.baseMapper.selectList(new QueryWrapper<Issue>());
        if(issues.size() != 0){
            return ResponseData.success(issues);
        }
        return ResponseData.error(500,"暂无信息");
    }

    private Serializable getKey(IssueParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Issue getOldEntity(IssueParam param) {
        return this.getById(getKey(param));
    }

    private Issue getEntity(IssueParam param) {
        Issue entity = new Issue();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
